﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Text;

namespace AutofacExtension
{
    public class RegistTypeResolver: Singleton<RegistTypeResolver>
    {

        public Dictionary<Type, List<Type>> Filters { get; set; }

        private RegistTypeResolver()
        {
            Filters = new Dictionary<Type, List<Type>>();
        }

        public ILifetimeScope LifetimeScope { get; set; }
        public T Resolve<T>()
        {
            if (LifetimeScope == null || !LifetimeScope.IsRegistered(typeof(T))) throw new Exception($"Type [{typeof(T)}] not registed");
            return LifetimeScope.Resolve<T>();
        }
        public object Resolve(Type type)
        {
            if (LifetimeScope == null || !LifetimeScope.IsRegistered(type)) throw new Exception($"Type [{type.FullName}] not registed");
            return LifetimeScope.Resolve(type);
        }
        public object Resolve(Type type, params Autofac.Core.Parameter[] parameters)
        {
            if (LifetimeScope == null || !LifetimeScope.IsRegistered(type)) throw new Exception($"Type [{type.FullName}] not registed");
            return LifetimeScope.Resolve(type, parameters);
        }
        public object ResolveNamed(string name, Type type)
        {
            if (LifetimeScope == null || !LifetimeScope.IsRegisteredWithName(name, type)) throw new Exception($"Named [{name}] with type [{type.FullName}] not registed");
            return LifetimeScope.ResolveNamed(name, type);
        }

        public T Resolve<T>(params Autofac.Core.Parameter[] parameters)
        {
            if (LifetimeScope == null || !LifetimeScope.IsRegistered(typeof(T))) throw new Exception($"Type [{typeof(T)}] not registed");
            return LifetimeScope.Resolve<T>(parameters);
        }
    }
}
